import Card from '../../../Common/Card/';
import {cbs} from '../../../../config/settings.js';

module.exports = {
  name: 'list',
  data(){
  	return{
       productList: [],
       listSize: 0,
       page: 1,
       size: 9,
       total: 0,
       // checkbox
       isManage: false,
       isIndeterminate: true,
       checkedPro: [],
       isIndeterminate: false,
       checkAll: false,
       searchText: '',
    }
  },
  components: {Card},
  methods: {
    getList() {
        let data = this.$$formData({
            brand: this.searchText,
            pageNum: this.page,
            pageSize: this.size,
        })
        this.$$api_product_list(data, res => {
            if(res.event === 0) {
                this.productList = res.obj;
                this.total = res.maxCount;
            }else {
                cbs.submitError.call(this, `获取商品信息失败，${res.msg}`);
            }
        })
    },
    handleCurrentChange(val) {
        this.page = val;
        this.getList();
    },
    changeEditStatus(item, value) {
        if(item.isEdit) {
            this.$set(item, 'isDisabled', true);

            this.updateQuantity(item.bucketTypeId, value)
                .then(num => {
                    console.log(num)
                    this.$set(item, 'isEdit', false);
                    this.$set(item, 'isDisabled', false);
                    num !== '' && this.$set(item, 'quantity', Number(num)) && cbs.submitSuccess.call(this, '修改成功');
                })
                .catch(err => {
                    cbs.submitError.call(this, `提交失败，${err}`);
                    this.$set(item, 'isDisabled', false);
                })
        }else {
            this.$set(item, 'isEdit', true);
        }
    },
    updateQuantity(id, value) {
        return new Promise((resolve, reject) => {
            if(!this.checkDigit(value)) {
                reject('请输入正整数值');
            }

            let data = this.$$formData({
                            waterBucketId: id,
                            quantity: value
                        });

            return this.$$api_product_edit(data, res => {
                if(res.event !== 0) {
                    cbs.submitError.call(this, `提交失败，${res.msg}`);
                    reject(res.msg);
                }else {
                    resolve(value);
                }
            })
        })
    },
    checkDigit(num) {
        num = num * 1;
        if(!Number.isInteger(num) || num < 0) {
            return false;
        }
        return true;
    },
    managePro() {
        this.isManage = !this.isManage;
        this.checkedPro = [];
    },
    deletePro() {
        let data = this.$$formData({
            waterBucketIds: this.proId
        })

        this.$$api_product_deletePro(data, res => {
            if(res.event === 0) {
                this.isManage = false;
                cbs.submitSuccess.call(this, '删除成功');
                this.getList();
            }else {
                cbs.submitError.call(this, `删除失败，${res.msg}`);
            }
        })
    },
    handleCheckAllChange(e) {
        this.checkedPro = e.target.checked ? this.productList : [];
        this.isIndeterminate = false;
    },
    handleCheckedProChange(v) {
        let count = v.length;
        this.checkAll = count === this.productList.length;
        this.isIndeterminate = count > 0 && count < this.productList.length;
    },
    onSearch() {
        if(this.searchText) {
          this.page = 1;
          this.getList();
        }
    },
    maskText(status) {
      switch(status) {
        case 1:
          return '该商品将于审核通过后发布';
        case 2:
          return '该商品审核失败';
      }
    }
  },
  created() {
      this.getList();
  },
  computed: {
      proId() {
          let ids = this.productList.map(item => {
              return item.bucketTypeId;
          })
          return ids;
      },
      isCheckedProEmp() {
          return this.checkedPro.length === 0;
      },
      proId() {
          let ids = this.checkedPro.map(p => {
              return p.bucketTypeId;
          })
          console.log(ids);
          return ids;
      },
  },
  mounted() {
  },

}
